# coding=utf-8
# -*- coding: UTF-8 -*-
#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.
import logging

from ksai.document.ocr.executor import OCRExecutor
from ksai.document.ocr.local_executor import LocalOCRExecutor
from ksai.persistence.storage.attachment import Attachment
from ksai.persistence.storage.metadata import read_metadata, write_metadata

logger = logging.getLogger(__name__)


class OCRExecutorFactory:
    executor: OCRExecutor = None

    def __init__(self):
        super().__init__()
        self.executor = LocalOCRExecutor()

    def execute(self, attachment: Attachment) -> str:
        metadata = read_metadata(attachment)
        if metadata and metadata.get('__content'):
            return metadata['__content']

        text = self.executor.execute(attachment.content)

        write_metadata(attachment, {
            '__content': text,
        })
        return text


ocr_factory = OCRExecutorFactory()
